<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class CI_Template {

    var $_values = array(); //cac bien se duoc day ra ngoai view
    var $_page_view = '';
    
    var $CI;
    var $config;
    var $template;
    var $regions = array(
        'ci_page_header'=>'',
        'ci_page_js'=>'',
        'ci_page_css'=>'',
        'ci_page_message'=>'',
        'ci_page_view'=>''
    );
    var $page_title = '';
    var $_doctype = array();
    var $_js = array();
    var $_css = array();
    var $_title = '';
    var $_meta = array();

    /**
     * Constructor
     *
     * Loads template configuration, template regions, and validates existence of
     * default template
     *
     * @access	public
     */
    
    public function __construct()
    {
        $this->CI = & get_instance();
        //$this->load->model('menu_model');
        $this->template = 'default';
        $this->load_regions();
        
    }

    /**
     * Render the master template or a single region
     *
     * @access    public
     * @param    string    optionally opt to render a specific region
     * @param    boolean    FALSE to output the rendered template, TRUE to return as a string. Always TRUE when $region is supplied
     * @return    void or string (result of template build)
     */
    function render() {
        $this->CI->load->library('parser');
        
        //load view va` set gia tri`
        $this->regions['ci_page_header'] = $this->build_header();
        $this->regions['ci_page_view'] = $this->build_content();
        $this->regions['ci_page_css'] = $this->build_css();
        $this->regions['ci_page_js'] = $this->build_js();
        
        $this->CI->parser->parse($this->template.'/index', $this->regions);
    }
    
    //load regions trong file config template
    function load_regions(){
        include(APPPATH . 'views/'.$this->template.'/config'.EXT);
        if (isset($template_regions)) {
            $result = array_merge($template_regions, $this->regions);
            $this->regions = $result;
        }
    }
    /**
     * Set regions for writing to
     *
     * @access  public
     * @param   array   properly formed regions array
     * @return  void
     */
    function set_regions($region_name = '', $view = '', $data = null, $includefile = true) {
        if($includefile){
            $view = str_replace("//", "/", $this->template.'/'.$view);
            if( isset($this->regions[$region_name]) ){
                $this->regions[$region_name] = $this->CI->load->view( $view , $data, true);
            }
        }else{
            if( isset($this->regions[$region_name]) ){
                $this->regions[$region_name] = $view;
            }
        }    
    }
    
    function set_template($template){
        $this->template = $template;
    }
    
    function set_page_title($title){
        $this->page_title = $title;
    }
    //set gia tri cho view
    function set($key, $value){
        $this->_values[$key] = $value;
    }
    
    //load file view de hien thi
    function set_view($view){
        $this->_page_view = str_replace("//", "/", $this->template.'/'.$view);
        
    }
    // --------------------------------------------------------------------

    function set_master_template($filename) {}

    function initialize($props) {}

    /**
     * Dynamically include javascript in the template
     *
     * NOTE: This function does NOT check for existence of .js file
     *
     * @access  public
     * @param   string   script to import or embed
     * @param   string  'import' to load external file or 'embed' to add as-is
     * @param   boolean  TRUE to use 'defer' attribute, FALSE to exclude it
     * @return  TRUE on success, FALSE otherwise
     */
    function add_js($script, $type = 'import', $defer = FALSE) {
        $success = TRUE;
        $js = NULL;

        switch ($type) {
            case 'import':                
                $js = '<script type="text/javascript" src="' . $script . '"';
                if ($defer) {
                    $js .= ' defer="defer"';
                }
                $js .= "></script>";
                break;

            case 'embed':
                $js = '<script type="text/javascript"';
                if ($defer) {
                    $js .= ' defer="defer"';
                }
                $js .= ">";
                $js .= $script;
                $js .= '</script>';
                break;

            default:
                $success = FALSE;
                break;
        }

        // Add to js array if it doesn't already exist
        if ($js != NULL) {
            $this->_js[] = $js;
        }
    }

    // --------------------------------------------------------------------

    /**
     * Dynamically include CSS in the template
     *
     * NOTE: This function does NOT check for existence of .css file
     *
     * @access  public
     * @param   string   CSS file to link, import or embed
     * @param   string  'link', 'import' or 'embed'
     * @param   string  media attribute to use with 'link' type only, FALSE for none
     * @return  TRUE on success, FALSE otherwise
     */
    function add_css($style, $type = 'link', $media = FALSE) {
        $success = TRUE;
        $css = NULL;

        switch ($type) {
            case 'link':

                $css = '<link type="text/css" rel="stylesheet" href="' . $style . '"';
                if ($media) {
                    $css .= ' media="' . $media . '"';
                }
                $css .= ' />';
                break;

            case 'import':
                $css = '<style type="text/css">@import url(' . $style . ');</style>';
                break;

            case 'embed':
                $css = '<style type="text/css">';
                $css .= $style;
                $css .= '</style>';
                break;

            default:
                $success = FALSE;
                break;
        }

        // Add to js array if it doesn't already exist
        if ($css != NULL ) {
            $this->_css[] = $css;
        }
        
    }

    public function add_meta($name, $content) {
        $meta = '<meta name="' . $name . '" content="' . $content . '" />';
        if ($meta != NULL) {
            $this->_meta[] = $meta;
        }
    }
    
    function build_css(){
        $css = '';
        
        if( count($this->_css) > 0 ){
            foreach($this->_css As $item){
                $css .= $item;
            }
        }
        
        return $css;
    }

    function build_js(){
        $js = '';
        
        if( count($this->_js) > 0 ){
            foreach($this->_js As $item){
                $js .= $item;
            }
        }
        return $js;
    }
    
    function build_meta(){
        $meta = '';
        if( count($this->_meta) > 0 ){
            foreach($this->_meta As $item){
                $meta .= $item;
            }
        }
        return $meta;
    }
    
    function set_title($name = ''){
        if($name != ''){
            $title = '<title>' . $name . '</title>';
            $this->_title = $title;
        }
    }
    
    function build_title(){
        if($this->_title != ''){
            return $this->_title;
        }else{
            $this->_title = '<title>duytrung2102@gmail.com</title>';
            return $this->_title;
        }
    }
    
    function build_header(){
        $header = '';
        
        $header .= $this->build_title();
        $header .= $this->build_meta();
        //$header .= $this->build_css();
        //$header .= $this->build_js();
        
        return $header;
    }
    
    function build_content(){
        if($this->_page_view != ''){
            return $this->CI->load->view( $this->_page_view ,$this->_values,true);
        }
        
        return '';
    }
    
}

// END Template Class

/* End of file Template.php */
/* Location: ./system/application/libraries/Template.php */